﻿using AutoMapper;
using Management.Systeam.Domaina;
using Management.Systeam.ErrorCode;
using Management.Systeam.Infrastructure.Interface;
using Management.Systeam.Read.Apis.Application.Command.DepartMentes;
using Management.Systeam.Read.Apis.Application.Command.ProductOutAllows;
using MediatR;

namespace Management.Systeam.Read.Apis.Application.CommandHandler.ProductOutAllows
{

    public class DelProductOutAllowCommandHandler : IRequestHandler<DelProductOutAllowCommand, ApiResult<int>>
    {
        IBaseRepository<ProductOutAllow> ProductOutAllow;
        IMapper mapper;

        public DelProductOutAllowCommandHandler(IBaseRepository<ProductOutAllow> ProductOutAllow, IMapper mapper)
        {
            this.ProductOutAllow = ProductOutAllow;
            this.mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(DelProductOutAllowCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> apiResult = new ApiResult<int>();
            try
            {
                var slist = ProductOutAllow.GetQuery().Where(x => x.Product_Id == request.ProductOutAllowtMent_Id).First();
                var list = mapper.Map(request, slist);
                var cout = ProductOutAllow.UpdateAsync(list).Result;
                if (cout > 0)
                {
                    apiResult.Code = ApiEnum.success;
                    apiResult.Message = "更新成功";
                    apiResult.Data = 1;
                }
                else
                {
                    apiResult.Code = ApiEnum.error;
                    apiResult.Message = "更新失败";
                    apiResult.Data = 0;
                }
            }

            catch (Exception ex)
            {
                apiResult.Code = ApiEnum.error;
                apiResult.Message = ex.Message;

            }

            return apiResult;
        }
    }
}
